package com.sky.service.common.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.mapper.common.DishFlavorMapper;
import com.sky.mapper.common.DishMapper;
import com.sky.mapper.common.SetmealDishMapper;
import com.sky.mapper.common.SetmealMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.DishFlavor;
import com.sky.pojo.Setmeal;
import com.sky.pojo.SetmealDish;
import com.sky.result.PageResult;
import com.sky.service.common.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    /**
     * 新增套餐
     *
     * @param dto
     */
    @Override

    @Transactional(rollbackFor = Exception.class)
    public void add(SetmealDTO dto) {
        Setmeal s = new Setmeal();
        BeanUtils.copyProperties(dto, s);
        setmealMapper.add(s);
        for (SetmealDish setmealDish : dto.getSetmealDishes()) {
            setmealDish.setSetmealId(s.getId());
        }
        setmealDishMapper.add(dto.getSetmealDishes());


        s.setStatus(0);

    }

    /**
     * 分页条件查询套餐
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<SetmealVO> p = setmealMapper.page(dto);

        return new PageResult(p.getTotal(), p.getResult());
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     * @return
     */
    @Override
    public boolean delete(List<Long> ids) {
        if (setmealMapper.statusCount(ids) > 0) {
            return false;
        }
        setmealMapper.delete(ids);
        setmealDishMapper.delete(ids);
        return true;
    }

    /**
     * 查询回显
     *
     * @param setmealId
     * @return
     */
    @Override
    public SetmealVO getById(Integer setmealId) {
        SetmealVO s = setmealMapper.getById(setmealId);
        s.setSetmealDishes(setmealDishMapper.getById(setmealId));
        return s;
    }

    /**
     * 修改套餐
     *
     * @param dto
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(SetmealDTO dto) {
        Setmeal s = new Setmeal();
        BeanUtils.copyProperties(dto, s);
        setmealMapper.update(s);
        setmealDishMapper.delete(Collections.singletonList(s.getId()));

        if (dto.getSetmealDishes() != null) {
            for (SetmealDish setmealDish : dto.getSetmealDishes()) {
                setmealDish.setSetmealId(dto.getId());
            }
            setmealDishMapper.add(dto.getSetmealDishes());
        }
    }

    /**
     * 启售/停售
     *
     * @param status
     * @param id
     * @return
     */
    @Override
    public boolean saleStatusChange(Integer status, Integer id) {
       if (setmealDishMapper.stopSaleCount(id)>0){
           return false;
       }
            setmealMapper.saleStatusChange(id,status);
        return true;
    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.getBycId(dish.getCategoryId().intValue());

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
